<?php
// +----------------------------------------------------------------------
// | Citrix [ WE ONLY DO WHAT IS NECESSARY ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018 http://www.tpCitrix.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.tpCitrix.com/ )
// +----------------------------------------------------------------------
// | Author: Citrix < 2722279500@qq.com >
// +----------------------------------------------------------------------

namespace app\admin\controller;
use app\admin\controller\Permissions;
use \think\Db;
use \think\Cookie;
use \think\Session;
use \think\Cache;
use app\admin\model\Fragment as fragmentModel;

class Fragment extends Permissions
{
	//内容碎片
	public function patch()
	{	
        $fragmentModel = Db::name('fragment');
		$fragment = $fragmentModel->field('id,fragment_name,image,parent_id')->order('id asc')->where(['parent_id'=>0])->select();//一级
		$where['parent_id'] = ['neq', 0];
		$parent_notempty = $fragmentModel->field('id,fragment_name,image,parent_id')->order('id asc')->where($where)->select();//二级
		if(!empty($fragment) || !empty($parent_notempty))//如果都不是空,组合成二维数组
		{
			foreach ($fragment as $e_k => $e_val) {//合并[二级,归分一级下]
				foreach ($parent_notempty as $n_k => $n_val) {
					if($fragment[$e_k]['id'] == $parent_notempty[$n_k]['parent_id'])
					{
						$fragment[$e_k]['all'][] = $parent_notempty[$n_k];
					}
				}
			}
		}else
		{
			$fragment = array_merge($fragment,$parent_notempty);
		}
        $this->assign('fragmentList',$fragment);
		return $this->fetch();
	}
	//内容碎片 编辑（添加）
	public function patch_publish()
	{
		$fragment = Db::name('fragment')->field('id,fragment_name')->where(['parent_id'=>0])->select();
		$this->assign('fragmentList',$fragment);
		//获取菜单id
    	$id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
    	$model = new fragmentModel();
		//是正常添加操作
		if($id > 0) {
    		//是修改操作
    		if($this->request->isPost()) {
    			//是提交操作
    			$post = $this->request->post();
    			if (!preg_match("/^[a-z0-9_]+$/", $post['mark']))
    			{
    				return $this->error('唯一标记,非法');
    			}
    		    $count_parent =	Db::name('fragment')->where('parent_id',$post['id'])->count();
    			if(!empty($count_parent) && !empty($post['parent_id']))
    			{
    				return $this->error('分组下有依赖,不能重新分组');
    			}
    			$where['id'] = ['neq', $post['id']];
				if(!empty($model->where('mark',$post['mark'])->where($where)->count()) )
    			{
    				return $this->error('唯一标记已存在');
    			}
    			if($post['parent_id'] == $post['id'])
    			{
    				return $this->error('分组不能添加自己');
    			}
	            //验证菜单是否存在
	            $patch = $model->where('id',$id)->find();
	            if(empty($patch)) {
	            	return $this->error('id不正确');
	            }
	            if(false == $model->allowField(true)->save($post,['id'=>$id])) {
	            	return $this->error('修改失败');
	            } else {
                    addlog($model->id);//写入日志
	            	return $this->success('修改成功','admin/Fragment/patch');
	            }
    		} else {
    			//非提交操作
    			$info = Db::name('fragment')->where(['id'=>$id])->find();
        		if(!empty($info)) {
                    $this->assign('info',$info);
                    return $this->fetch();
                } else {
                    return $this->error('id不正确');
                }
    		}
    	} else {
    		//是新增操作
    		if($this->request->isPost()) {
    			//是提交操作
    			$post = $this->request->post();
    			if (!preg_match("/^[a-z0-9_]+$/", $post['mark']))
    			{
    				return $this->error('唯一标记,非法');
    			}
    			if(!empty($model->where('mark',$post['mark'])->count()))
    			{
    				return $this->error('唯一标记已存在');
    			}
	            if(false == $model->allowField(true)->save($post)) {
	            	return $this->error('添加失败');
	            } else {
                    addlog($model->id);//写入日志
	            	return $this->success('添加成功','admin/Fragment/patch');
	            }
    		} else {
    			//非提交操作
    			return $this->fetch();
    		}
    	}
		return $this->fetch();
	}
	//删除碎片
	public function patch_delete()
    {
    	if($this->request->isAjax()) {
    		$id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
            if(false == Db::name('fragment')->where('id',$id)->delete()) {
                return $this->error('删除失败');
            } else {
                addlog($id);//写入日志
                return $this->success('删除成功','admin/Fragment/patch');
            }
    	}
    }

    
    
}




?>